package leetcode.view0067;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int strToInt(String str) {
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			if (c == ' ')
				continue;
			if (c != '-' && (c < '0' || c > '9') && c != '+') {
				return 0;
			}
			long num = 0;
			int mul = 1;
			if (c == '-') {
				mul = -1;
			} else if (c != '+') {
				num = c - '0';
			}
			i++;
			while (i < str.length() && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
				num = num * 10 + str.charAt(i) - '0';
				i++;
				if ((mul > 0 && num >= Integer.MAX_VALUE) || (mul < 0 && mul * num <= Integer.MIN_VALUE)) {
					break;
				}
			}
			if (mul > 0 && num >= Integer.MAX_VALUE) {
				return Integer.MAX_VALUE;
			}
			if (mul < 0 && mul * num <= Integer.MIN_VALUE) {
				return Integer.MIN_VALUE;
			}
			return (int) num * mul;
		}
		return 0;
	}

	@Override
	public void debug4() {
		H.compare(-123, this.strToInt("-123adf123"));
		H.compare(1, this.strToInt("+1"));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
